package org.jeecg.modules.electricity.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.electricity.entity.LwgyFreeCharge;
import org.jeecg.modules.electricity.entity.LwgyMfdl;
import org.jeecg.modules.electricity.service.ILwgyMfdlCzService;
import org.jeecg.modules.electricity.service.ILwgyMfdlService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 免费电量管理
 * @Author: jeecg-boot
 * @Date:   2022-06-14
 * @Version: V1.0
 */
@Api(tags="免费电量管理")
@RestController
@RequestMapping("/electricity/lwgyMfdl")
@Slf4j
public class LwgyMfdlController extends JeecgController<LwgyMfdl, ILwgyMfdlService> {
	@Autowired
	private ILwgyMfdlService lwgyMfdlService;

	 @Autowired
	 private ILwgyMfdlCzService lwgyMfdlCzService;
	
	/**
	 * 分页列表查询
	 *
	 * @param lwgyFreeCharge
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "免费电量管理-分页列表查询")
	@ApiOperation(value="免费电量管理-分页列表查询", notes="免费电量管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyFreeCharge lwgyFreeCharge,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyFreeCharge> queryWrapper = QueryGenerator.initQueryWrapper(lwgyFreeCharge, req.getParameterMap());
		Page<LwgyFreeCharge> page = new Page<LwgyFreeCharge>(pageNo, pageSize);
		IPage<LwgyFreeCharge> pageList = lwgyMfdlCzService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param lwgyMfdl
	 * @return
	 */
	@AutoLog(value = "免费电量管理-添加")
	@ApiOperation(value="免费电量管理-添加", notes="免费电量管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyMfdl lwgyMfdl) {
		lwgyMfdlService.save(lwgyMfdl);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param lwgyMfdl
	 * @return
	 */
	@AutoLog(value = "免费电量管理-编辑")
	@ApiOperation(value="免费电量管理-编辑", notes="免费电量管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyMfdl lwgyMfdl) {
		lwgyMfdlService.updateById(lwgyMfdl);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "免费电量管理-通过id删除")
	@ApiOperation(value="免费电量管理-通过id删除", notes="免费电量管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyMfdlService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "免费电量管理-批量删除")
	@ApiOperation(value="免费电量管理-批量删除", notes="免费电量管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyMfdlService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "免费电量管理-通过id查询")
	@ApiOperation(value="免费电量管理-通过id查询", notes="免费电量管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyMfdl lwgyMfdl = lwgyMfdlService.getById(id);
		if(lwgyMfdl==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyMfdl);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyMfdl
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyMfdl lwgyMfdl) {
        return super.exportXls(request, lwgyMfdl, LwgyMfdl.class, "免费电量管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyMfdl.class);
    }

	/**
	* 充值免费电
	*
	* @param id
	* @return
	*/
	@AutoLog(value = "充值免费电")
	@ApiOperation(value = "充值免费电", notes = "充值免费电")
	@PostMapping(value = "/rechargeMfDl")
	public Result<?> rechargeMfDl(@RequestBody LwgyFreeCharge freeCharge) {

		String education = freeCharge.getStudentEducation();
		String mfDl = freeCharge.getMfDl();
		String xl = freeCharge.getXl();
		List<LwgyFreeCharge> chargeRoomList = lwgyMfdlCzService.queryFreeCharge(education);
		for (LwgyFreeCharge item : chargeRoomList) {
			item.setMfDl(mfDl);
			item.setXl(xl);
		}
		// 数据存储到表中
		lwgyMfdlCzService.saveBatch(chargeRoomList);
		int mfDls = Integer.parseInt(mfDl);
		int counts = Integer.parseInt(freeCharge.getStudentCount());
		double je = (counts * mfDls) * 0.55;

		return Result.OK("充值成功!");
	}
 }
